Skip to content

mgbec/digtw-not-agentcore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Digital Twin

An AI-powered digital twin application that combines a Next.js frontend with a FastAPI backend, deployed on AWS using Terraform. The system leverages AWS Bedrock's Amazon Nova models for intelligent conversations with persistent session memory.

πŸ“‹ Project Overview

Digital Twin is a full-stack application that provides an AI course companion capable of:

  • Real-time chat interactions with context-aware responses
  • Session-based conversation memory (stored in S3)
  • Lambda-optimized backend deployment
  • Dual backend support: Traditional FastAPI + AWS Bedrock Agentcore
  • Responsive, modern UI with TypeScript and Tailwind CSS
  • Multi-environment support (dev, test, prod)

πŸ—οΈ Architecture

Dual Backend Architecture

This project supports two backend implementations running in parallel:

1. Traditional Backend (/chat endpoint)

Frontend β†’ API Gateway β†’ Lambda (FastAPI)
                           ↓
                    Direct Bedrock API
                           ↓
                    Manual S3 Memory

2. Agentcore Backend (/agentcore/chat endpoint) - NEW! πŸŽ‰

Frontend β†’ API Gateway β†’ Lambda (FastAPI)
                           ↓
                    Bedrock Agent
                           ↓
                    Built-in Memory Service

Full Architecture Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Next.js Frontend (port 3000)               β”‚
β”‚         React 19.2.1 + TypeScript + Tailwind            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                    HTTP/HTTPS API
                            β”‚
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚                       β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Traditional    β”‚    β”‚   Agentcore      β”‚
        β”‚ Lambda Backend β”‚    β”‚  Lambda Backend  β”‚
        β”‚   (/chat)      β”‚    β”‚ (/agentcore/*)   β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚                      β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Direct Bedrock β”‚    β”‚  Bedrock Agent   β”‚
        β”‚   API Calls    β”‚    β”‚  Orchestration   β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚                      β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚  S3 Memory     β”‚    β”‚  Built-in Memory β”‚
        β”‚  (Manual)      β”‚    β”‚   (Automatic)    β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚                      β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
                    β”‚  CloudFront β”‚
                    β”‚    Cache    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

twin/
β”œβ”€β”€ frontend/                    # Next.js React application
β”‚   β”œβ”€β”€ app/                    # App Router pages
β”‚   β”‚   β”œβ”€β”€ layout.tsx         # Root layout
β”‚   β”‚   β”œβ”€β”€ page.tsx           # Home page
β”‚   β”‚   └── globals.css        # Global styles
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   └── twin.tsx           # Main chat component
β”‚   β”œβ”€β”€ public/                # Static assets
β”‚   β”œβ”€β”€ package.json           # Dependencies
β”‚   └── next.config.ts         # Next.js configuration
β”‚
β”œβ”€β”€ backend/                     # FastAPI Python backend
β”‚   β”œβ”€β”€ server.py              # Traditional FastAPI app
β”‚   β”œβ”€β”€ lambda_handler.py      # Traditional Lambda entry point
β”‚   β”œβ”€β”€ agentcore_server.py    # Agentcore FastAPI app (NEW)
β”‚   β”œβ”€β”€ agentcore_lambda_handler.py  # Agentcore Lambda entry point (NEW)
β”‚   β”œβ”€β”€ agentcore_config.py    # Agent configuration (NEW)
β”‚   β”œβ”€β”€ context.py             # AI prompt context
β”‚   β”œβ”€β”€ resources.py           # Shared utilities
β”‚   β”œβ”€β”€ deploy.py              # Lambda packaging script
β”‚   β”œβ”€β”€ requirements.txt        # Python dependencies
β”‚   β”œβ”€β”€ .env.agentcore         # Agentcore environment template (NEW)
β”‚   β”œβ”€β”€ data/                  # Static data files
β”‚   β”‚   β”œβ”€β”€ facts.json         # Knowledge base
β”‚   β”‚   β”œβ”€β”€ style.txt          # Response style guidelines
β”‚   β”‚   └── summary.txt        # System summary
β”‚   └── lambda-package/        # Packaged Lambda deployment
β”‚
β”œβ”€β”€ terraform/                   # Infrastructure as Code
β”‚   β”œβ”€β”€ main.tf                # Traditional backend resources
β”‚   β”œβ”€β”€ agentcore.tf           # Agentcore backend resources (NEW)
β”‚   β”œβ”€β”€ agentcore_variables.tf # Agentcore variables (NEW)
β”‚   β”œβ”€β”€ variables.tf           # Input variables
β”‚   β”œβ”€β”€ versions.tf            # Provider versions
β”‚   β”œβ”€β”€ output.tf              # Output values
β”‚   β”œβ”€β”€ terraform.tfvars       # Environment-specific vars
β”‚   └── terraform.tfstate.d/   # State storage backends
β”‚
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ deploy.sh              # Main deployment script (Bash)
β”‚   β”œβ”€β”€ deploy.ps1             # Alternative deployment (PowerShell)
β”‚   └── upload_knowledge_base.sh  # Prepare Agentcore agent (NEW)
β”‚
β”œβ”€β”€ memory/                      # Local session memory (dev)
β”‚   └── *.json                 # Session conversation history
β”‚
β”œβ”€β”€ README.md                      # This file (start here!)
β”œβ”€β”€ DOCUMENTATION_INDEX.md         # Complete documentation guide (NEW)
β”œβ”€β”€ DECISION_TREE.md               # Choose the right backend (NEW)
β”œβ”€β”€ BACKEND_COMPARISON.md          # Compare Traditional vs Agentcore (NEW)
β”œβ”€β”€ QUICK_START_AGENTCORE.md      # Deploy Agentcore in 3 steps (NEW)
β”œβ”€β”€ AGENTCORE_MIGRATION_GUIDE.md  # Detailed deployment guide (NEW)
β”œβ”€β”€ AGENTCORE_COST_OPTIMIZED.md   # Cost breakdown & optimization (NEW)
└── CHANGELOG.md                   # Version history (NEW)

πŸš€ Quick Start

Prerequisites

  • Node.js 18+ (frontend)
  • Python 3.13+ (backend)
  • AWS CLI configured with credentials
  • Docker (for Lambda package building)
  • Terraform 1.0+ (for infrastructure)
  • uv (Python package manager, recommended)

Local Development

1. Frontend Setup

cd frontend
npm install
npm run dev

Visit http://localhost:3000 to view the application.

2. Backend Setup

cd backend

# Create a .env file
cat > .env << EOF
CORS_ORIGINS=http://localhost:3000
BEDROCK_MODEL_ID=amazon.nova-lite-v1:0
DEFAULT_AWS_REGION=us-east-1
USE_S3=false
MEMORY_DIR=../memory
EOF

# Install dependencies
pip install -r requirements.txt
# or with uv:
uv sync

# Run development server
uvicorn server:app --reload --port 8000

The API will be available at http://localhost:8000.

3. Test the Integration

Open the frontend, type a message in the chat, and watch it communicate with your local backend.

πŸ› οΈ Configuration

Environment Variables

Frontend (frontend/.env.local):

NEXT_PUBLIC_API_URL=http://localhost:8000

Backend (backend/.env):

# AWS Configuration
DEFAULT_AWS_REGION=us-east-1
BEDROCK_MODEL_ID=amazon.nova-lite-v1:0

# CORS
CORS_ORIGINS=http://localhost:3000,https://yourdomain.com

# Storage
USE_S3=false                    # Set to true for production
S3_BUCKET=twin-memory-bucket
MEMORY_DIR=../memory           # Local path for dev

# Bedrock Model Options (pick one):
# amazon.nova-micro-v1:0   (fastest, cheapest)
# amazon.nova-lite-v1:0    (balanced - default)
# amazon.nova-pro-v1:0     (most capable, higher cost)

Terraform Variables (terraform/terraform.tfvars)

project_name           = "twin"
environment            = "dev"
default_aws_region     = "us-east-1"
use_custom_domain      = false
root_domain            = ""
bedrock_model_id       = "amazon.nova-lite-v1:0"

🚒 Deployment

Deploy to AWS

# Deploy to dev environment (includes both backends)
./scripts/deploy.sh dev

# Deploy to production
./scripts/deploy.sh prod twin

What the deployment does:

  1. βœ… Packages the backend into a Lambda-compatible ZIP
  2. βœ… Initializes Terraform state in S3
  3. βœ… Creates/updates AWS infrastructure:
    • Traditional Backend: Lambda function with FastAPI/Mangum
    • Agentcore Backend: Lambda + Bedrock Agent (NEW)
    • API Gateway for HTTP routing (both backends)
    • S3 bucket for conversation memory
    • CloudFront CDN (optional)
    • IAM roles and policies
  4. βœ… Deploys the frontend to CloudFront/S3
  5. βœ… Outputs API endpoints and frontend URL

Deployment Requirements

  • AWS account with appropriate permissions (Lambda, API Gateway, S3, IAM, Bedrock, Bedrock Agents)
  • S3 backend bucket for Terraform state: twin-terraform-state-{ACCOUNT_ID}
  • AWS Bedrock model access in your region
  • AWS Bedrock Agents access (for Agentcore backend)

πŸ†• Agentcore Quick Start

For detailed Agentcore deployment instructions, see:

  • Quick Start: QUICK_START_AGENTCORE.md
  • Full Guide: AGENTCORE_MIGRATION_GUIDE.md
  • Cost Details: AGENTCORE_COST_OPTIMIZED.md
# Deploy both backends
cd terraform
terraform apply

# Both endpoints will be available:
# Traditional: https://your-api/chat
# Agentcore:   https://your-api/agentcore/chat

πŸ“¦ Key Dependencies

Frontend

  • Next.js 16.0.7 - React framework with server-side rendering
  • React 19.2.1 - UI library
  • Tailwind CSS 4 - Utility-first CSS framework
  • Lucide React - Icon library
  • TypeScript 5 - Type-safe JavaScript

Backend

  • FastAPI - Modern Python web framework
  • Uvicorn - ASGI server
  • Boto3 - AWS SDK for Python
  • Mangum - ASGI-to-Lambda adapter
  • PyPDF - PDF processing (if needed)
  • Python Multipart - Multipart form data handling

πŸ” Security Considerations

CVE-2025-55182 Mitigation βœ“

This project has been patched against the critical React Server Components RCE vulnerability:

  • Next.js: 16.0.7 (patched from 16.0.3)
  • React: 19.2.1 (patched from 19.2.0)
  • React-DOM: 19.2.1 (patched from 19.2.0)

Best Practices

  • βœ… CORS configured for specific origins
  • βœ… S3 buckets with public access blocked
  • βœ… IAM roles with least-privilege permissions
  • βœ… Environment variables for sensitive configuration
  • βœ… Session IDs validated on each request

πŸ§ͺ Testing

Frontend Tests

cd frontend
npm run lint

Backend Tests

cd backend
# (Add pytest tests as needed)
pytest tests/

πŸ“š API Documentation

Once the backend is running, visit the interactive API documentation (Swagger UI):

  • Traditional backend: http://localhost:8000/docs
  • Agentcore backend: http://localhost:8001/docs

Traditional Backend Endpoint

POST /chat

  • Request:
    {
      "message": "Tell me about AI",
      "session_id": "optional-session-uuid"
    }
  • Response:
    {
      "response": "AI is...",
      "session_id": "generated-or-provided-uuid"
    }

Agentcore Backend Endpoint

POST /agentcore/chat

  • Request:
    {
      "message": "Tell me about AI",
      "session_id": "optional-session-uuid"
    }
  • Response:
    {
      "response": "AI is...",
      "session_id": "generated-or-provided-uuid",
      "agent_type": "agentcore",
      "trace": {...}  // Optional trace data
    }

Backend Comparison

Feature Traditional (/chat) Agentcore (/agentcore/chat)
Memory Manual S3 management Automatic (built-in)
Context Loaded at runtime Embedded in agent
Orchestration Single API call Multi-agent capable
Tracing Manual logging Built-in trace data
Cost ~$5-20/month ~$5-20/month

πŸ—‚οΈ Conversation Memory

Traditional Backend

  • Development (Local): Conversations stored in memory/ directory as JSON files
  • Production (S3): Sessions saved to S3 bucket (manual management)

Agentcore Backend

  • All Environments: Built-in Bedrock Agent memory service (automatic)
  • No manual S3 management required
  • Session persistence handled by AWS

🎯 Choosing Between Backends

Both backends are deployed and available. Choose based on your needs:

Use Traditional Backend (/chat) When:

  • βœ… You want proven, stable technology
  • βœ… You need full control over the conversation flow
  • βœ… You're comfortable with manual memory management
  • βœ… You want the simplest possible architecture

Use Agentcore Backend (/agentcore/chat) When:

  • βœ… You want built-in conversation memory
  • βœ… You need multi-agent orchestration capabilities
  • βœ… You want better tracing and debugging
  • βœ… You're building complex agentic workflows
  • βœ… You want AWS-native agent management

Cost Comparison

Both backends have similar costs (~$5-20/month usage-based):

  • Traditional: Lambda + Bedrock API calls + S3 storage
  • Agentcore: Lambda + Bedrock Agent + Bedrock API calls

Note: We use embedded context instead of Knowledge Bases to avoid OpenSearch Serverless costs (~$175/month).

πŸ“Š For detailed comparison, see BACKEND_COMPARISON.md

πŸ› Troubleshooting

Frontend Cannot Connect to Backend

  1. Check NEXT_PUBLIC_API_URL in frontend .env.local
  2. Verify backend is running: curl http://localhost:8000/docs
  3. Check CORS configuration in backend/server.py
  4. Review browser console for exact error messages

Lambda Deployment Fails

  1. Verify Docker is running: docker --version
  2. Check AWS credentials: aws sts get-caller-identity
  3. Ensure Bedrock access: aws bedrock list-foundation-models --region us-east-1
  4. Review Lambda package size (must be < 50MB for direct upload)

Bedrock API Errors

  • Model IDs may require regional prefix (e.g., us.amazon.nova-lite-v1:0)
  • Ensure Bedrock is available in your AWS region
  • Check IAM permissions for bedrock:InvokeModel

Agentcore Agent Not Found

  1. Verify Terraform deployed successfully: cd terraform && terraform output agentcore_agent_id
  2. Check agent status in AWS Console: Bedrock > Agents
  3. Ensure agent is prepared: ./scripts/upload_knowledge_base.sh dev
  4. Verify IAM permissions for bedrock:InvokeAgent

Context Not Loading in Agentcore

  1. Context is embedded during Terraform deployment from backend/data/ files
  2. If you update facts/style/summary, run terraform apply again
  3. Check agent instructions in AWS Console to verify context is present

πŸ“– Documentation Links

Project Documentation

  • πŸ“‘ Documentation Index: DOCUMENTATION_INDEX.md - Complete guide to all docs
  • 🌳 Decision Tree: DECISION_TREE.md - Quick decision guide
  • βš–οΈ Backend Comparison: BACKEND_COMPARISON.md - Detailed feature comparison
  • πŸš€ Agentcore Quick Start: QUICK_START_AGENTCORE.md - Deploy in 3 steps
  • πŸ“– Agentcore Migration Guide: AGENTCORE_MIGRATION_GUIDE.md - Full deployment guide
  • πŸ’° Cost Optimization: AGENTCORE_COST_OPTIMIZED.md - Cost breakdown & savings
  • πŸ“ Changelog: CHANGELOG.md - Version history

External Documentation

🀝 Contributing

  1. Create a feature branch: git checkout -b feature/my-feature
  2. Make changes and test locally
  3. Commit with clear messages: git commit -m "Add feature X"
  4. Push and create a pull request

πŸ“ License

This project is private. All rights reserved.

πŸ‘€ Author

Created by: mgbec
Repository: digtw

πŸ†˜ Support

For issues or questions:

  1. Check the troubleshooting section above
  2. Review backend logs: aws logs tail /aws/lambda/twin-dev-backend --follow
  3. Check frontend browser console for client-side errors
  4. Review GitHub Issues or contact the maintainer

πŸ†• What's New

Version 0.2.0 - Agentcore Support

  • ✨ Added AWS Bedrock Agentcore backend (parallel deployment)
  • ✨ Dual backend architecture: Traditional + Agentcore
  • ✨ Cost-optimized: No Knowledge Base (embedded context)
  • ✨ Built-in conversation memory with Agentcore
  • ✨ Multi-agent orchestration ready
  • πŸ“š Comprehensive Agentcore documentation

Version 0.1.0 - Initial Release

  • πŸŽ‰ Traditional FastAPI backend with Bedrock
  • πŸŽ‰ Next.js frontend with TypeScript
  • πŸŽ‰ Terraform infrastructure as code
  • πŸŽ‰ Multi-environment support

Last Updated: December 2025
Version: 0.2.0

About

not using Agentcore

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors